//
//  RewardViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/6/30.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "RewardViewController.h"
#import "RewardCell.h"
#import "UserDetailViewController.h"

#import "GreatGodViewModel.h"
#import "GreatGodModel.h"

@interface RewardViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) GreatGodViewModel * greatViewModel;


@end

@implementation RewardViewController

-(GreatGodViewModel *) greatViewModel{
    if (!_greatViewModel) {
        _greatViewModel  = [[GreatGodViewModel alloc] init];
    }
    return _greatViewModel;
}

-(UITableView *) tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, DEF_CONTENT_INTABBAR_HEIGHT) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        
        [_tableView setTableHeaderView:[self tabHaderView]];
        
        [MXUtility setExtraCellLineHidde:_tableView tabHeader:YES tabFooter:NO];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem setTitle:@"奖励榜"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.tableView];
    /**
     奖励榜网络请求
     */
    [self powerNetWork];
}

-(void) powerNetWork{
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    [[self.greatViewModel findUserBuyInfoRequestParameter:@"2"] subscribeNext:^(id x) {
        
        [self.tableView reloadData];
        [MXUtility hideMBProgress:self.view];
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
    }];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.greatViewModel.finduserArrayList count];
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * str = @"cell";
    RewardCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"RewardCell" owner:self options:nil] lastObject];
    }
    
    GreatGodModel * model = [self.greatViewModel.finduserArrayList objectAtIndex:indexPath.row];
    [cell setRewardCellDataSource:model index:indexPath];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UserDetailViewController * userdetail = [[UserDetailViewController alloc] init];
    GreatGodModel * model = [self.greatViewModel.finduserArrayList objectAtIndex:indexPath.row];
    [userdetail setUserID:model.greatuserId];
    [self.navigationController pushViewController:userdetail animated:YES];
}

-(UIView *)tabHaderView{
    /**
     第一个视图
     */
    UIView * view  = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, 40)];
    [view setBackgroundColor:[UIColor whiteColor]];
    
    
    NSMutableArray * arrayOne = [NSMutableArray arrayWithObjects:@"名次／头像",@"昵称",@"积分", nil];
    
    
    for (NSInteger i = 0; i < [arrayOne count]; i++) {
        
        UILabel * label = [[UILabel alloc] init];
        [label setFrame:CGRectMake(i * (view.width / [arrayOne count]), 0, view.width / [arrayOne count], view.height)];
        [label setBackgroundColor:[UIColor whiteColor]];
        [label setText:[arrayOne objectAtIndex:i]];
        [label setFont:[UIFont systemFontOfSize:14.0f]];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setTextColor:[UIColor darkGrayColor]];
        [view addSubview:label];

    }
   
    return view;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
